package com.yimidida.library.tool;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.yimidida.library.base.LibraryApplication;

/**
 * Create on: 9/14/21
 * by Mr.Zhu
 * Description: TODO
 */
public class ShareDataUtils {

    /**
     * 上次断开连接时间
     */
    public static final String LAST_DISCONNECT_TIME = "last_disconnect_time";


    private static SharedPreferences sharedPreferences;

    public static SharedPreferences getShare() {
        if (sharedPreferences == null) {
            sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(LibraryApplication.getContext());
        }
        return sharedPreferences;
    }


    /**
     * 保存上次断开连接时间
     *
     * @return
     */
    public static void saveLastDisconnectTime(long lastDisconnectTime) {
        getShare().edit().putLong(LAST_DISCONNECT_TIME, lastDisconnectTime).commit();
//        final EasyPhotos easyPhotos = new EasyPhotos();
    }

    /**
     * 获取上次断开连接时间
     *
     * @return
     */
    public static long getLastDisconnectTime() {
        long lastDisconnectTime = getShare().getLong(LAST_DISCONNECT_TIME, 0);
        return lastDisconnectTime;
    }


    /**
     * 保存数据到文件
     *
     * @param key
     * @param data
     */
    public static void saveData(String key, Object data) {

        String type = data.getClass().getSimpleName();

        SharedPreferences.Editor editor = getShare().edit();
        if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) data);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) data);
        } else if ("String".equals(type)) {
            editor.putString(key, (String) data);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) data);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) data);
        }
        editor.commit();
    }

    /**
     * 从文件中读取数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public static Object getData(String key, Object defValue) {
        String type = defValue.getClass().getSimpleName();
        SharedPreferences sharedPreferences = getShare();
        //defValue为为默认值，如果当前获取不到数据就返回它
        if ("Integer".equals(type)) {
            return sharedPreferences.getInt(key, (Integer) defValue);
        } else if ("Boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        } else if ("String".equals(type)) {
            return sharedPreferences.getString(key, (String) defValue);
        } else if ("Float".equals(type)) {
            return sharedPreferences.getFloat(key, (Float) defValue);
        } else if ("Long".equals(type)) {
            return sharedPreferences.getLong(key, (Long) defValue);
        }
        return null;
    }

    /**
     * 移除存储数据
     * @param key
     */
    public static void deleteDate(String key) {
        SharedPreferences.Editor edit = getShare().edit();
        edit.remove(key);
        edit.commit();
    }


}
